package com.lix.langrensha.utils;

import com.lix.langrensha.contants.CommonStatusCode;

/**
 * @Author lix
 * @create 2019/12/11-14:55
 * @description
 */
public class RtnJson<T> {

    private String message = "";

    private T data;

    private String status = CommonStatusCode.S00000000.getCode();

    public void setData(T data) {
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public RtnJson() {
    }

    public RtnJson(T data) {
        this.data = data;
    }

    public RtnJson(String message) {
        this.message = message;
    }

    public RtnJson(String message, String status) {
        this.message = message;
        this.status = status;
    }

    public RtnJson(CommonStatusCode code) {
        this.message = code.getMessage();
        this.status = code.getCode();
    }


    public boolean isSuccess() {
        return this.status == CommonStatusCode.S00000000.getCode();
    }

    public static RtnJson toSuccess() {
        return RtnJson.toSuccess(null);
    }


    public static RtnJson toSuccess(Object data) {
        RtnJson result = new RtnJson(CommonStatusCode.S00000000);
        if (data != null)
            result.setData(data);
        return result;
    }

    public static RtnJson toSuccessFormat(String data,String format) {
        RtnJson result = new RtnJson(CommonStatusCode.S00000000);
        result.setMessage(String.format(data, format));
        return result;
    }

    public static RtnJson toFail(CommonStatusCode errorCode) {
        if (null == errorCode) {
            errorCode = CommonStatusCode.E00020006;
        }
        return new RtnJson(errorCode);
    }

    public static RtnJson toFailFormat(CommonStatusCode errorCode,String format) {
        if (null == errorCode) {
            errorCode = CommonStatusCode.E00020006;
        }
        RtnJson rtnJson = new RtnJson(errorCode);
        rtnJson.setMessage(String.format(rtnJson.getMessage(), format));
        return rtnJson;
    }

}
